package omq.samples;

import java.io.IOException;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import omq.api.*;
import omq.api.logger.OmqLogger;

/*
 *  OMQ Sample Codes:
 *  -----------------
 * 
 *  SendMessage demonstrates how to send a String as a message to the queue
 *  It uses SOAP to establish connection with the server.
 *  Every message has a messageID and so does the message you are sending, you can retrieve it using sess.getLastMessageID()

 *  Author  	  	: Hagai Izenberg
 *  Creating Date 	: 02-11-2008
 *  Company 		: OMQI
 */

public class SendMessage {

	public static void main(String[] args) throws ServiceException, IOException
	{
		String omqUser = "demo-superadmin";
		String omqPassword = "demokopp";
		String omqQueueManager = "bank";
		String omqQueue = "q1";
		
		OMQConnection con = OMQConnectionFactory.getConnection(omqUser,omqPassword, new OMQQueueManager(omqQueueManager));
		OMQSession sess = con.createSession(false);
		String stringSent = "hello world";
		OMQMessage msgSent = new OMQMessage(stringSent);
		System.out.println("sending message...");
		
		int rc = sess.sendMessage(new OMQQueue(omqQueue), msgSent);
		if (rc == 0)
		{
			System.out.println("message sent successfully");
			System.out.println("sent message ID = "+sess.getLastMessageID());
		}
		else if (rc == 101) // example for handling specific error code 101 = Authentication failed 
		{
			System.out.println("Authentication failed...");
		}
		else
		{
			System.err.println("send message failed");
		}
		System.out.println("LastErrorCode()="+sess.getLastErrorCode()+", LastErrorDescription()="+sess.getLastErrorDescription());
		
		/*
		 * check 
		 * http://www.onlinemq.com/support/index.php/Online_MQ_API#Error_handling 
		 * for a list of error codeS
		 */ 
	}
}
